import Base from "./Base";
export default class HashHistory extends Base {
  constructor(router) {
    super(router);
    this.router = router;
    // 默认hash路由下 看看有没有默认值
    this.ensureURL();
  }
  // 监听hash值的变化
  setupListeners() {
    window.addEventListener("hashchange", () => {
      this.transitionTo(this.getCurrentLocation());
    });
  }
  ensureURL() {
    if (window.location.hash) {
      return;
    }
    // 没有的话给一个默认的/
    window.location.hash = "/";
  }
  getCurrentLocation() {
    return window.location.hash.slice(1);
  }
  push(location) {
    this.transitionTo(location, () => {
      window.location.hash = location;
    });
  }
}
